package com.hrm.system.service;

import com.hrm.common.exception.CommonException;
import com.hrm.common.entity.PermissionConstants;
import com.hrm.common.entity.ResultCode;
import com.hrm.common.service.BaseService;
import com.hrm.common.util.BeanMapUtils;
import com.hrm.common.util.IdWorker;
import com.hrm.domain.system.Permission;
import com.hrm.domain.system.PermissionApi;
import com.hrm.domain.system.PermissionMenu;
import com.hrm.domain.system.PermissionPoint;
import com.hrm.system.dao.PermissionApiDao;
import com.hrm.system.dao.PermissionDao;
import com.hrm.system.dao.PermissionMenuDao;
import com.hrm.system.dao.PermissionPointDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PermissionService extends BaseService<Permission> {

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private PermissionApiDao permissionApiDao;

    @Autowired
    private PermissionMenuDao permissionMenuDao;

    @Autowired
    private PermissionPointDao permissionPointDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 保存权限
     */
    public void addPermission(Map<String, Object> map) throws Exception {
        String id = idWorker.nextId() + "";
        Permission permission = BeanMapUtils.mapToBean(map, Permission.class);
        permission.setId(id);
        //根据permision的type来保存对应的permission类型
        switch (permission.getType()) {
            case PermissionConstants.PY_API:
                PermissionApi api = BeanMapUtils.mapToBean(map, PermissionApi.class);
                api.setId(permission.getId());
                permissionApiDao.save(api);
                break;
            case PermissionConstants.PY_MENU:
                PermissionMenu menu = BeanMapUtils.mapToBean(map, PermissionMenu.class);
                menu.setId(permission.getId());
                permissionMenuDao.save(menu);
                break;
            case PermissionConstants.PY_POINT:
                PermissionPoint point = BeanMapUtils.mapToBean(map, PermissionPoint.class);
                point.setId(permission.getId());
                permissionPointDao.save(point);
                break;
            default:
                throw new CommonException(ResultCode.FAIL);
        }
        permissionDao.save(permission);
    }

    /**
     * 更新权限
     */
    public void updatePermission(Map<String, Object> map) throws Exception {
        //传递参数map取出permission对象
        Permission temp = BeanMapUtils.mapToBean(map, Permission.class);
        //根据id查询原权限
        Permission permission = permissionDao.findById((String) map.get("id")).get();
        //更新权限数据
        permission.setName(temp.getName());
        permission.setCode(temp.getCode());
        permission.setDescription(temp.getDescription());
        permission.setEnVisible(temp.getEnVisible());
        //根据permision的type来更新对应的permission类型
        switch (permission.getType()) {
            case PermissionConstants.PY_API:
                PermissionApi api = BeanMapUtils.mapToBean(map, PermissionApi.class);
                api.setId(permission.getId());
                permissionApiDao.save(api);
                break;
            case PermissionConstants.PY_MENU:
                PermissionMenu menu = BeanMapUtils.mapToBean(map, PermissionMenu.class);
                menu.setId(permission.getId());
                permissionMenuDao.save(menu);
                break;
            case PermissionConstants.PY_POINT:
                PermissionPoint point = BeanMapUtils.mapToBean(map, PermissionPoint.class);
                point.setId(permission.getId());
                permissionPointDao.save(point);
                break;
            default:
                throw new CommonException(ResultCode.FAIL);
        }
        permissionDao.save(permission);
    }

    /**
     * id查询权限
     * 1、查询权限
     * 2、根据类型查询权限资源
     * 3、构造map
     */
    public Map<String, Object> findPermissionById(String id) {
        Permission permission = permissionDao.findById(id).get();
        Integer type = permission.getType();
        Object obj = null;
        switch (type){
            case PermissionConstants.PY_MENU:
                obj = permissionMenuDao.findById(id).get();
                break;
            case PermissionConstants.PY_POINT:
                obj = permissionPointDao.findById(id).get();
                break;
            case PermissionConstants.PY_API:
                obj = permissionApiDao.findById(id).get();
                break;
            default:
                throw new CommonException(ResultCode.FAIL);
        }
        Map<String, Object> map = BeanMapUtils.beanToMap(obj);
        map.put("id",permission.getId());
        map.put("code",permission.getCode());
        map.put("description",permission.getDescription());
        map.put("name",permission.getName());
        map.put("type",permission.getType());

        return map;
    }

    /**
     * 动态查询全部权限列表
     * type:0=1+2 ，1，2，3
     * enVisible：0查询所有saas平台最高权限，1查询企业权限
     * parentId：父id
     */
    public List<Permission> findPermissions(Map<String, Object> map) {
        return permissionDao.findAll(setSpec4Permission(map));
    }

    /**
     * id删除权限
     * 1、删除权限
     * 2、删除权限对应类型资源
     */
    public void deletePermission(String id) {
        //查询权限
        Integer type = permissionDao.findById(id).get().getType();
        //根据类型查询资源
        switch (type){
            case PermissionConstants.PY_MENU:
                permissionMenuDao.deleteById(id);
                break;
            case PermissionConstants.PY_POINT:
                permissionPointDao.deleteById(id);
                break;
            case PermissionConstants.PY_API:
                permissionApiDao.deleteById(id);
                break;
            default:
                throw new CommonException(ResultCode.FAIL);
        }
        permissionDao.deleteById(id);
    }
}
